package cn.demomaster.quickpicturer.sample;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.demomaster.quickpicturer.config.ImageConfig;
import cn.demomaster.quickpicturer.config.ImageConfigBuilder;
import cn.demomaster.quickpicturer.constants.ImageSourceType;
import cn.demomaster.quickpicturer.picker.QuickPicturer;
import cn.demomaster.quickpicturer.activity.CameraIDCardActivity;
import cn.demomaster.quickpicturer.activity.PictureSelectorActitity;

public class MainActivity extends AppCompatActivity {

    //PicturerView picturerView;
    CheckBox cb_camera;
    CheckBox cb_gallery,cb_camera_crop,cb_gallery_crop,cb_custom_gallery,cb_custom_gallery_crop;
    EditText et_image_count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        picturerView = findViewById(R.id.picturerView);
//        picturerView.setImageDrawable(getDrawable(R.mipmap.wallpaper));

        cb_camera = findViewById(R.id.cb_camera);
        cb_gallery = findViewById(R.id.cb_gallery);
        cb_camera_crop = findViewById(R.id.cb_camera_crop);
        cb_gallery_crop = findViewById(R.id.cb_gallery_crop);
        cb_custom_gallery = findViewById(R.id.cb_custom_gallery);
        cb_custom_gallery_crop = findViewById(R.id.cb_custom_gallery_crop);
        et_image_count = findViewById(R.id.et_image_count);
        findViewById(R.id.btn_test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, PictureSelectorActitity.class));//ImagePickerActivity.class));
            }
        });
        Button btn_01 = findViewById(R.id.btn_01);
        btn_01.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, PictureSelectActivity.class));
            }
        });
        Button btn_02 = findViewById(R.id.btn_02);
        btn_02.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, PictureSelectorActitity.class));

            }
        });
        Button btn_03 = findViewById(R.id.btn_03);
        btn_03.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, CameraIDCardActivity.class));

            }
        });
        Button btn_04 = findViewById(R.id.btn_04);
        btn_04.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                List<ImageSourceType> ImageSourceTypeList = new ArrayList<>();
                List<String> ImageSourceTypeNamesList = new ArrayList<>();
                if(cb_camera.isChecked()){
                    ImageSourceTypeList.add(ImageSourceType.CAMERA);
                    ImageSourceTypeNamesList.add("拍照");
                }
                if(cb_camera_crop.isChecked()){
                    ImageSourceTypeList.add(ImageSourceType.CAMERA_CROP);
                    ImageSourceTypeNamesList.add("拍照并截取");
                }
                if(cb_gallery.isChecked()){
                    ImageSourceTypeList.add(ImageSourceType.GALLERY);
                    ImageSourceTypeNamesList.add("从相册选取");
                }
                if(cb_gallery_crop.isChecked()){
                    ImageSourceTypeList.add(ImageSourceType.GALLERY_CROP);
                    ImageSourceTypeNamesList.add("从相册选取并裁剪");
                }
                if(cb_custom_gallery.isChecked()){
                    ImageSourceTypeList.add(ImageSourceType.CUSTOM_GALLERY);
                    ImageSourceTypeNamesList.add("从自定义相册选取");
                }
                if(cb_custom_gallery_crop.isChecked()){
                    ImageSourceTypeList.add(ImageSourceType.CUSTOM_GALLERY_CROP);
                    ImageSourceTypeNamesList.add("从自定义相册选取并裁剪");
                }

                int imageCount = 1;
                if(!TextUtils.isEmpty(et_image_count.getText())) {
                    imageCount = Integer.parseInt(et_image_count.getText().toString());
                }
                ImageSourceType[] ImageSourceType = ImageSourceTypeList.toArray(new ImageSourceType[ImageSourceTypeList.size()]);
                String[] ImageSourceTypeNames = ImageSourceTypeNamesList.toArray(new String[ImageSourceTypeNamesList.size()]);
                ImageConfigBuilder imageConfigBuilder = new ImageConfigBuilder(MainActivity.this);
                imageConfigBuilder.setSources(ImageSourceType);

                ImageConfig config = imageConfigBuilder.setCropHeight(100)
                        .setCropHeight(100)
                        .setSources(ImageSourceType)
                        .setSourceNames(ImageSourceTypeNames)
                        .setMaxImages(imageCount)
                        .build();
                QuickPicturer.getInstance().selectImages(config, new QuickPicturer.OnImageSelectedListener() {
                    @Override
                    public void onImagesSelected(Uri[] images) {
                        Toast.makeText(MainActivity.this, "onImagesSelected" + images.length, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        QuickPicturer.getInstance().onActivityResult(MainActivity.this,requestCode, resultCode, data);
    }
}